<?php
//*********************************** 
//Universal Media Publishing Platform
//Adding news,tracks,artists,albums,categories
//*********************************** 

$datype = "add";

//Подключение необходимых файлов
require_once "config.php";
require_once "engine/login.php";
require_once "engine/func.php";

switch ($_GET["type"]){
	case "post":
		$addtype = "post";
	break;
	case "track":
		$addtype = "track";
	break;
	case "album":
		$addtype = "album";
	break;
	case "category":
		$addtype = "category";
	break;
	case "static":
		$addtype = "static";
	break;
    default:
		$addtype = "post";
}

//Считывание категорий из БД
$query = "SELECT * FROM category ORDER BY caid ";
$result1 = mysql_query($query);
while($row1 = mysql_fetch_array($result1)){
   $f[] =$row1;
}
for ($x = 0; $x < count($f); $x++) {
    $category[$f[$x]["caid"]] = $f[$x];
}

//Считывание альбомов из БД    
$query = "SELECT * FROM albums ORDER BY alid ";
$res = mysql_query($query);
while($row = mysql_fetch_array($res)){
   $albums[] =$row;
}
 
//Считывание исполнителей из БД    
$query = "SELECT * FROM artist ORDER BY arid ";
$res = mysql_query($query);
while($row = mysql_fetch_array($res)){
   $artist[] =$row;
}

require 'engine/meta.php';//Подключаем обработчик мета тегов

if ((check($login, $pass))&&($usrgroup==4)){
	require 'template/'.$tpl.'/mainu.tpl';
	if ($addtype=="post") require 'template/'.$tpl.'/addnews.tpl';
	if ($addtype=="track") require 'template/'.$tpl.'/addtrack.tpl';
	if ($addtype=="album") require 'template/'.$tpl.'/addalbum.tpl';
	if ($addtype=="category") require 'template/'.$tpl.'/addcat.tpl';
	if ($addtype=="static") require 'template/'.$tpl.'/addstatic.tpl';
	
	if (isset($_POST['send'])){
		if ($addtype=="post"){
			$date = date("Y-m-d");
			$title = $_POST['title'];
			$title = str_replace("'", "&#039;", $title);
			$description = $_POST['description'];
			$keywords = $_POST['keywords'];
			$short = $_POST['short'];
			$full = $_POST['full'];
			$cat = $_POST['cat'];
			if (!$_POST['url']==""){
				$url=$_POST['url'];
			}
			else{
				$url=TitleToURL($_POST['title']);
			}
			if ($_POST['onmain']==1) $onmain = "1"; else $onmain = "0";
			$query = "INSERT INTO news (`date`, `title`, `url`, `short`, `full`, `description`, `keywords`, `cat`, `onmain`, `poid`, `views`, `author`, `type`, `artist`, `year`, `genre`, `album`, `alid`, `length`, `file`, `image`, `trno`) 
			VALUES ('$date', '$title', '$url', '$short', '$full', '$description', '$keywords', '$cat', '$onmain', NULL, '0', '$login', 'post', '', '', '', '', '', '0:00', '', '', '')";
			$res = mysql_query($query);
		}
			
		if ($addtype=="category"){
			$title = $_POST['title'];
			$title = str_replace("'", "&#039;", $title);
			if (!$_POST['url']==""){
				$url=$_POST['url'];
			}
			else{
				$url=TitleToURL($_POST['title']);
			}
			$description = $_POST['description'];
			$keywords = $_POST['keywords'];
			$information = $_POST['information'];
			$query = "INSERT INTO category (`title`, `url`, `description`, `keywords`, `information`) 
			VALUES ('$title', '$url', '$description', '$keywords', '$information')";
			$res = mysql_query($query);
		}
		
		if ($addtype=="static"){
			$title = $_POST['title'];
			$title = str_replace("'", "&#039;", $title);
			if (!$_POST['url']==""){
				$url=$_POST['url'];
			}
			else{
				$url=TitleToURL($_POST['title']);
			}
			$description = $_POST['description'];
			$keywords = $_POST['keywords'];
			$content = $_POST['content'];
			$query = "INSERT INTO static (`title`, `url`, `description`, `keywords`, `content`, `views`, `author`) 
			VALUES ('$title', '$url', '$description', '$keywords', '$content', '0', '$login')";
			$res = mysql_query($query);
		}
		
		if ($addtype=="track"){
			$date = date("Y-m-d");
			$title = $_POST['title'];
			$title = str_replace("'", "&#039;", $title);
			if (!$_POST['url']==""){
				$url=$_POST['url'];
			}
			else{
				$url=TitleToURL($_POST['title']);
			}
			$trno = $_POST['trno'];
			if ($_POST['onmain']==1) $onmain = "1"; else $onmain = "0";
			$cat = $_POST['cat'];
			$description = $_POST['description'];
			$keywords = $_POST['keywords'];
			$artist = $_POST['artist'];
			$year = $_POST['year'];
			$genre = $_POST['genre'];
			$alid = substr($_POST['alid'], 0, strpos($_POST['alid'],"/"));//Poluchaem aid
			$album = substr($_POST['alid'], strlen($alid)+1, strlen($_POST['alid'])-strlen($alid)-1);//Poluchaem album
			$album = str_replace("'", "&#039;", $album);//Ubiraem '
			$length = $_POST['length'];
			$file = $_POST['file'];
			$image = $_POST['image'];
			$query = "INSERT INTO news (`date`, `title`, `url`, `short`, `full`, `description`, `keywords`, `cat`, `onmain`, `poid`, `views`, `author`, `type`, `artist`, `year`, `genre`, `album`, `alid`, `length`, `file`, `image`, `trno`) 
			VALUES ('$date', '$title', '$url', '', '', '$description', '$keywords', '$cat', '$onmain', NULL, '0', '$login', 'track', '$artist', '$year', '$genre', '$album', '$alid', '$length', '$file', '$image', '$trno')";
			$res = mysql_query($query);
		}
	}
}
else {
$message = "У вас нет прав добавлять новости";
require 'template/'.$tpl.'/mainu.tpl';
} 
require 'template/'.$tpl.'/maind.tpl';
?>
            
